package com.Thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description 异步基础学习
 * @Author Weseave
 * @Date 2023/2/20 23:45
 * @Version 1.0
 */
public class CompletableFutureStudy {
    public static final ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main (String[] args) throws ExecutionException, InterruptedException {
        //1. 没有返回值
//        CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//        }, executor);

//        //2. 有返回值
//        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor);
//        // get是阻塞等结果
//        System.out.println("completableFuture异步结果："+ future2.get());

        //3. 计算完成时回调方法，方法完成后的处理(主要是感知)
//        CompletableFuture<Integer> future3 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).whenComplete((result,exception)->{
//            // 虽然能得到异常信息，但是没法修改返回数据
//            System.out.println("completableFuture异步结果："+ result +";异常结果是:"+ exception);
//        }).exceptionally(throwable -> {//R apply(T t)
//            // 可以感知异常，同时返回默认值
//            return 10;
//        });
//        System.out.println("completableFuture异步结果："+ future3.get());

        //3. 方法执行完成后处理，不论成功还是失败
//        CompletableFuture<Integer> future4 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).handle((result,exception)->{  //R apply(T t,U u)
//            if(result !=null){
//                return result * 2;
//            }
//            if(exception != null) {
//                return 0;
//            }
//            return 0;
//        });
//        System.out.println("completableFuture异步结果："+ future4.get());

        CompletableFuture<Void> task1 = CompletableFuture.runAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        }, executor);

        CompletableFuture<Void> task2 = CompletableFuture.runAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        }, executor);

        CompletableFuture.allOf(task1,task2);

        System.out.println("健身房的了；就算了；啊 ");
    }
}
